package com.example.zhoutao.puzzle.Login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.zhoutao.puzzle.Data.UserData;
import com.example.zhoutao.puzzle.Data.UserDataManager;
import com.example.zhoutao.puzzle.R;

/**
 * Create By ZT on 2019.4.12
 * @Description 注册功能
 */
public class Register extends AppCompatActivity {

    private EditText mAccount;
    private EditText mPwd;
    private EditText mPwdCheck;
    private Button mSureBtn;
    private Button mCancalBtn;
    private UserDataManager mUserDataManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mAccount=findViewById(R.id.et_reg_name);
        mPwd=findViewById(R.id.et_reg_pwd);
        mPwdCheck=findViewById(R.id.et_reg_pwdAgain);
        mSureBtn=findViewById(R.id.btn_reg_sure);
        mCancalBtn=findViewById(R.id.btn_reg_cancel);

        //按钮监听
        mSureBtn.setOnClickListener(m_register_Listener);
        mCancalBtn.setOnClickListener(m_register_Listener);

        //建立本地数据库
        if (mUserDataManager==null){
            mUserDataManager=new UserDataManager(this);
            mUserDataManager.openDataBase();
        }

    }

    View.OnClickListener m_register_Listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn_reg_sure:
                    register_check();
                    break;

                case R.id.btn_reg_cancel:
                    Intent intent=new Intent(Register.this,Login.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };

    public void register_check(){
        if(isUserNameAndPwdValid()){
            String userName=mAccount.getText().toString().trim();
            String userPwd=mPwd.getText().toString().trim();
            String userPwdCheck=mPwdCheck.getText().toString().trim();

            //检查用户是否存在
            int count=mUserDataManager.findUserByName(userName);
            if (count>0){
                Toast.makeText(this, "用户【(tom)】已存在，请重新输入！", Toast.LENGTH_SHORT).show();
                return ;
            }

            //检查两次密码输入是否一致
            if (userPwd.equals(userPwdCheck)==false){
                Toast.makeText(this, "两次密码输入不一致，请重新输入密码！", Toast.LENGTH_SHORT).show();
                return ;
            }else{
                UserData mUser=new UserData(userName,userPwd);
                mUserDataManager.openDataBase();
                long flag=mUserDataManager.insertUserData(mUser);
                if (flag == -1) {
                    Toast.makeText(this, "注册失败，请重新尝试！", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "注册成功！", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Register.this,Login.class);
                    startActivity(intent);
                    finish();
                }
            }


        }
    }

    public boolean isUserNameAndPwdValid(){
        if (mAccount.getText().toString().trim().equals("")){
            Toast.makeText(this, "用户名为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwd.getText().toString().trim().equals("")){
            Toast.makeText(this, "密码为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwdCheck.getText().toString().trim().equals("")){
            Toast.makeText(this, "密码确认为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
